package com.itfreer.setting.configsyn;

import com.itfreer.gis.styles.StylesSynService;
import com.itfreer.metadata.metadata.MetadataSynService;
import com.itfreer.metadata.organize.OrganizeSynService;
import com.itfreer.metadata.version.DataVersionService;

/**
 * 定义配置同步服务类
 */
public class ConfigSynService {

    /**
     * 处理事件
     */
    public interface ProgressListener {
        /**
         * 处理进度
         * 当size和count值一样时，表示已完成
         * @param count
         * @param size
         */
        void onSize(int count, int size, String msg);

        /**
         * 失败时
         */
        void onFail(String info);

        /**
         * 成功完成
         */
        void onSuccess();
    }

    private boolean isRuning = false;

    public static final ConfigSynService instance = new ConfigSynService();
    private ConfigSynService() {
    }

    public boolean getIsRuning() {
        return isRuning;
    }

    /**
     * 配置同步
     * @param listener
     */
    public void synConfig(final ConfigSynService.ProgressListener listener, final Boolean showMsg, final Boolean qzUpdate) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(listener!=null){
                    isRuning = true;
                    if(qzUpdate || DataVersionService.instance.needUpdateVersion()){
                        try {
                            listener.onSize(9,1, "开始下载政区配置！");
                            if(OrganizeSynService.instance.synAdministrative()){
                                listener.onSize(9,1, "下载政区配置成功！");
                                Thread.sleep(1000);
                            } else {
                                listener.onSize(9,1, "下载政区配置失败！");
                                isRuning = false;
                                return;
                            }

                            listener.onSize(9,2, "开始下载机构配置！");
                            if(OrganizeSynService.instance.synOrganization()){
                                listener.onSize(9,2, "下载机构配置成功！");
                                Thread.sleep(1000);
                            }else{
                                listener.onSize(9,2, "下载机构配置失败！");
                                isRuning = false;
                                return;
                            }

                            listener.onSize(9,3, "开始下载部门配置！");
                            if(OrganizeSynService.instance.synOrganization()){
                                listener.onSize(9,3, "下载部门配置成功！");
                                Thread.sleep(1000);
                            }else{
                                listener.onSize(9,3, "下载部门配置失败！");
                                isRuning = false;
                                return;
                            }

                            listener.onSize(9,5, "开始下载元数据配置！");
                            if(MetadataSynService.instance.synMetadata()){
                                listener.onSize(9,5, "下载元数据成功！");
                                Thread.sleep(1000);
                            }else {
                                listener.onSize(9,5, "下载元数据失败！");
                                isRuning = false;
                                return;
                            }

                            listener.onSize(9,6, "开始下载字典配置！");
                            if(MetadataSynService.instance.synDictionary()){
                                listener.onSize(9,6, "下载字典成功！");
                                Thread.sleep(1000);
                            }else{
                                listener.onSize(9,6, "下载字典失败！");
                                isRuning = false;
                                return;
                            }

                            listener.onSize(9,7, "开始下载数据定义！");
                            if(MetadataSynService.instance.synDefinition()){
                                listener.onSize(9,7, "下载数据定义成功！");
                                Thread.sleep(1000);
                            }else{
                                listener.onSize(9,7, "下载数据定义失败！");
                                isRuning = false;
                                return;
                            }

                            listener.onSize(9,8, "开始下载地图配置！");
                            if(StylesSynService.instance.synStyles()){
                                listener.onSize(9,8, "下载地图成功！");
                                Thread.sleep(1000);
                            }else{
                                listener.onSize(9,8, "下载地图失败！");
                                isRuning = false;
                                return;
                            }

                            listener.onSize(9,9, "配置同步完成！");
                            listener.onSuccess();
                            DataVersionService.instance.updateSuccess();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        if(showMsg) {
                            listener.onSize(1,1, "配置同步完成！");
                        } else {
                            listener.onSize(9,9, "配置同步完成！");
                        }
                        listener.onFail("无需更新配置");
                    }
                    isRuning = false;
                }
            }
        }).start();
    }
}
